home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / ISSUE08 / MODEL / ELECTRIC.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-01-13  |  7.6 KB  |  317 lines

  1. {*******************************************************
  2.                 'Electronic' Components
  3.  
  4. This unit contains the code for the the CustomElectric,
  5. TConductor, TBattery, TSwitch, TResistor and TLamp
  6. components. These components comprise a class of system
  7. modelling components designed as a demo of what you can do
  8. with the Delphi VCL.
  9.  
  10. They ARE NOT imtended to fully model electronic systems
  11. at this time. They would need to be much more sophisticated.
  12. At a minimum they would need a GetResistance method so
  13. they could 'sense' the resistance (back pressure if you like)
  14. of the circuit 'downstream'. Also, a TJunction component
  15. would be required to allow parallel circuits.
  16.  
  17. What these components ARE designed for is to demonstrate how
  18. components can be connected together to create complicated
  19. systems from simple building blocks.
  20.  
  21. It has been my experience that this approach drastically
  22. reduces development time and the frustration of debugging
  23. complicated code.
  24.  
  25. These components are freeware. Use them as you please. The
  26. only thing I ask is you publish as freeware anything you
  27. develop directly from this code. You can also e-mail me
  28. any improvements you make on these components.
  29.  
  30. I am specifically interested in any system to make components
  31. available to the user at run time from some kind of pallet.
  32.  
  33.                 Paul Warren
  34.        HomeGrown Software Development
  35.      (c) 1995 Langley British Columbia.
  36.               (604) 530-9097
  37.        e-mail:  hg_soft@uniserve.com
  38.   Home page: http://haven.uniserve.com/~hg_soft
  39.  
  40. ********************************************************}
  41.  
  42. unit Electric;
  43.  
  44. interface
  45.  
  46. uses
  47.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  48.   Forms, Dialogs;
  49.  
  50. type
  51.   TCustomElectric = class(TComponent) { base class }
  52.   private
  53.     { Private declarations }
  54.     FCurrent: LongInt;
  55.     FPotential: LongInt;
  56.     FOutPipe: TCustomElectric;
  57.     FAfterProcess: TNotifyEvent;
  58.     procedure SetCurrent(Value: LongInt);
  59.     procedure SetPotential(Value: LongInt);
  60.     procedure SetAfterProcess(Value: TNotifyEvent);
  61.     procedure Engine; virtual;
  62.   protected
  63.     { Protected declarations }
  64.     procedure After; dynamic;
  65.   public
  66.     { Public declarations }
  67.     constructor Create(AOwner: TComponent); override;
  68.     procedure Run; virtual; abstract;
  69.     property OutPipe: TCustomElectric read FOutPipe write FOutPipe;
  70.   published
  71.     { Published declarations }
  72.     property Current: LongInt read FCurrent write SetCurrent default 0;
  73.     property Potential: LongInt read Fpotential write SetPotential default 0;
  74.     property AfterProcess: TNotifyEvent read FAfterProcess write SetAfterProcess;
  75.   end;
  76.  
  77.   TConductor = class(TCustomElectric)
  78.   private
  79.     { Private declarations }
  80.   protected
  81.     { Protected declarations }
  82.   public
  83.     { Public declarations }
  84.     constructor Create(AOwner: TComponent); override;
  85.     procedure Run; override;
  86.   published
  87.     { Published declarations }
  88.     property OutPipe;
  89.   end;
  90.  
  91.   TBattery = class(TConductor)
  92.   private
  93.     { Private declarations }
  94.     FLifetime: integer;
  95.   protected
  96.     { Protected declarations }
  97.   public
  98.     { Public declarations }
  99.     constructor Create(AOwner: TComponent); override;
  100.     procedure Run; override;
  101.   published
  102.     { Published declarations }
  103.     property OutPipe;
  104.     property Lifetime: integer read FLifetime write FLifetime default 100;
  105.   end;
  106.  
  107.   TSwitch = class(TConductor)
  108.   private
  109.     { Private declarations }
  110.     FEnabled: Boolean;
  111.   protected
  112.     { Protected declarations }
  113.   public
  114.     { Public declarations }
  115.     constructor Create(AOwner: TComponent); override;
  116.     destructor Destroy; override;
  117.     procedure Run; override;
  118.   published
  119.     { Published declarations }
  120.     property OutPipe;
  121.     property Enabled: Boolean read FEnabled write FEnabled default false;
  122.   end;
  123.  
  124.   TResistor = class(TConductor)
  125.   private
  126.     { Private declarations }
  127.     FResistance: LongInt;
  128.   protected
  129.     { Protected declarations }
  130.   public
  131.     { Public declarations }
  132.     constructor Create(AOwner: TComponent); override;
  133.     procedure Engine; override;
  134.     procedure Run; override;
  135.   published
  136.     { Published declarations }
  137.     property OutPipe;
  138.     property Resistance: LongInt read FResistance write FResistance default 20;
  139.   end;
  140.  
  141.   TLamp = class(TConductor)
  142.   private
  143.     { Private declarations }
  144.     FLampOn: boolean;
  145.     FRating: byte;
  146.   protected
  147.     { Protected declarations }
  148.   public
  149.     { Public declarations }
  150.     constructor Create(AOwner: TComponent); override;
  151.     procedure Engine; override;
  152.     procedure Run; override;
  153.   published
  154.     { Published declarations }
  155.     property OutPipe;
  156.     property LampOn: boolean read FLampOn write FLampOn default false;
  157.     property Rating: byte read FRating write FRating default 5;
  158.   end;
  159.  
  160. procedure Register;
  161.  
  162. implementation
  163.  
  164. { base class }
  165. constructor TCustomElectric.Create(AOwner: TComponent);
  166. begin
  167.   inherited Create(AOwner);
  168.   FOutPipe := nil;
  169. end;
  170.  
  171. procedure TCustomElectric.SetCurrent(Value: LongInt);
  172. begin
  173.   FCurrent := Value;
  174. end;
  175.  
  176. procedure TCustomElectric.SetPotential(Value: LongInt);
  177. begin
  178.   FPotential := Value;
  179. end;
  180.  
  181. procedure TCustomElectric.SetAfterProcess(Value: TNotifyEvent);
  182. begin
  183.   FAfterProcess := Value;
  184. end;
  185.  
  186. procedure TCustomElectric.After;
  187. begin
  188.   if Assigned(FAfterProcess) then FAfterProcess(Self);
  189. end;
  190.  
  191. procedure TCustomElectric.Engine;
  192. begin
  193.   After;
  194. end;
  195.  
  196. constructor TConductor.Create(AOwner: TComponent);
  197. begin
  198.   inherited Create(AOwner);
  199.   FOutPipe := nil;
  200. end;
  201.  
  202. procedure TConductor.Run;
  203. begin
  204.   if not (OutPipe is TBattery) then
  205.   begin
  206.     Engine;
  207.     OutPipe.Current := Current;
  208.     OutPipe.Potential := Potential;
  209.     OutPipe.Run;
  210.   end;
  211. end;
  212.  
  213. constructor TBattery.Create(AOwner: TComponent);
  214. begin
  215.   inherited Create(AOwner);
  216.   FLifetime := 100;
  217. end;
  218.  
  219. procedure TBattery.Run;
  220. begin
  221.   if not (OutPipe is TBattery) then
  222.   begin
  223.     Engine;
  224.     OutPipe.Current := Current;
  225.     OutPipe.Potential := Potential;
  226.     OutPipe.Run;
  227.   end;
  228. end;
  229.  
  230. constructor TSwitch.Create(AOwner: TComponent);
  231. begin
  232.   inherited Create(AOwner);
  233.   FEnabled := false;
  234. end;
  235.  
  236. destructor TSwitch.Destroy;
  237. begin
  238.   FEnabled := false;
  239.   inherited Destroy;
  240. end;
  241.  
  242. procedure TSwitch.Run;
  243. begin
  244.   if not (OutPipe is TBattery) and Enabled then
  245.   begin
  246.     Engine;
  247.     OutPipe.Current := Current;
  248.     OutPipe.Potential := Potential;
  249.     OutPipe.Run;
  250.   end else
  251.   begin
  252.     Engine;
  253.     OutPipe.Current := 0;
  254.     OutPipe.Potential := 0;
  255.     OutPipe.Run;
  256.   end;
  257. end;
  258.  
  259. constructor TResistor.Create(AOwner: TComponent);
  260. begin
  261.   inherited Create(AOwner);
  262.   FResistance := 20;
  263. end;
  264.  
  265. procedure TResistor.Engine;
  266. begin
  267.   Potential := Potential-(Current*Resistance);
  268.   inherited Engine;
  269. end;
  270.  
  271. procedure TResistor.Run;
  272. begin
  273.   if not (OutPipe is TBattery) then
  274.   begin
  275.     Engine;
  276.     OutPipe.Current := Current;
  277.     OutPipe.Potential := Potential;
  278.     OutPipe.Run;
  279.   end;
  280. end;
  281.  
  282. constructor TLamp.Create(AOwner: TComponent);
  283. begin
  284.   inherited Create(AOwner);
  285.   FLampOn := false;
  286.   FRating := 5;
  287. end;
  288.  
  289. procedure TLamp.Engine;
  290. begin
  291.   if Current > Rating then LampOn := true
  292.   else LampOn := false;
  293.   inherited Engine;
  294. end;
  295.  
  296. procedure TLamp.Run;
  297. begin
  298.   if not (OutPipe is TBattery) then
  299.   begin
  300.     Engine;
  301.     OutPipe.Current := Current;
  302.     OutPipe.Potential := Potential;
  303.     OutPipe.Run;
  304.   end;
  305. end;
  306.  
  307. procedure Register;
  308. begin
  309.   RegisterComponents('Misc', [TConductor]);
  310.   RegisterComponents('Misc', [TBattery]);
  311.   RegisterComponents('Misc', [TSwitch]);
  312.   RegisterComponents('Misc', [TResistor]);
  313.   RegisterComponents('Misc', [TLamp]);
  314. end;
  315.  
  316. end.
  317.